import {post} from "@/utils/http.js";
import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import useAccountStore from "@/store/modules/AccountStore.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import request from "@/utils/request.js";

export const addBgAPI = (imgUrl) => {
    return post(
        '/bg/add',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            imgUrl: rsaPubEncrypt(imgUrl),
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const getBgAPI = (current, pageSize) => {
    const desKey = genDesKey()
    return request.get(
        '/bg/page',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey),
                current, pageSize,
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const delBgAPI = (id) => {
    return post(
        '/bg/del',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}